#include <myhead.h>
#include "page.h"

void* page_to_group_index(){
	print_nav2("首页/群聊功能", (char*)client_loginname());
	printf("[指令提示]:\n");
	printf("\t1-发送群消息\n");
	printf("\t2-查询我的群聊\n");
	printf("\t3-查询群成员\n");
	printf("\t4-创建群聊\n");
	printf("\t5-添加群成员\n");
	printf("\tP-返回上一页\n");
	char ch=getInputChar(NULL);
	switch(ch){
	case '1':
		return page_to_group_chat;
	case '2':
		return page_to_group_mygrp;
	case '3':
		return page_to_group_mem;
	case '4':
		return page_to_group_create;
	case '5':
		return page_to_group_add;
	case 'P':
		return page_to_index;
	}
	print_red("错误的指令");
	return page_to_group_index;
}

void* page_to_group_chat(pkg_t* retPkg, char* errmsg){
	printf("[请输入群名]");
	char grp[1024]={0};
	fgets(grp,sizeof(grp),stdin);
	grp[strlen(grp)-1]=0;
	printf("[请输入消息]");
	char msg[1024]={0};
	fgets(msg,sizeof(msg),stdin);
	msg[strlen(msg)-1]=0;
	//
	//打包                                                                                            
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_GRP_CHAT,3,(char*)client_loginname(),grp, msg);
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);
	if(ret != 0){
		print_red2("通信异常", errmsg);
		return page_to_group_index;
	}
	if(strcmp(pkg_get(retPkg, 0), "OK") != 0){
		print_red2("群发消息失败", pkg_get(retPkg, 1));
		return page_to_group_index;
	}
	//
	char buf[1024]={0};
	sprintf(buf,"群发消息完成\n已经向[%s]个在线用户发送了消息(%s)，尚余[%s]个离线用户未发(%s)\n",
			pkg_get(retPkg,1), pkg_get(retPkg,2),pkg_get(retPkg,3),pkg_get(retPkg,4));
	print_green(buf);
	return page_to_group_index;
}

void* page_to_group_mygrp(pkg_t* retPkg, char* errmsg){
	//打包                                                                                            
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_GRP_MYGRP,1,(char*)client_loginname());
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);
	if(ret != 0){
		print_red2("通信异常", errmsg);
		return page_to_group_index;
	}
	if(strcmp(pkg_get(retPkg, 0), "OK") != 0){
		print_red2("查询我的群失败", pkg_get(retPkg, 1));
	}
	//打印我的群
	char buf[128]={0};
	sprintf(buf,"我所在的群有[%s]个", pkg_get(retPkg, 1));
	print_green(buf);
	printf("%s\n", pkg_get(retPkg, 2));
	return page_to_group_index; 
}

void* page_to_group_mem(pkg_t* retPkg, char* errmsg){
	printf("[请输入群名]");
	char grp[1024]={0};
	fgets(grp,sizeof(grp),stdin);
	grp[strlen(grp)-1]=0;
	//打包                                                                                            
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_GRP_MEM,1, grp);
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);
	if(ret != 0){
		print_red2("通信异常", errmsg);
		return page_to_group_index;
	}
	if(strcmp(pkg_get(retPkg, 0), "OK") != 0){
		print_red2("获取群成员失败", pkg_get(retPkg, 1));
	}
	//打印我的群
	char buf[128]={0};
	sprintf(buf,"该用共有[%s]个成员，群成员列表如下:", pkg_get(retPkg, 1));
	print_green(buf);
	printf("%s\n", pkg_get(retPkg, 2));
	return page_to_group_index; 
}

void* page_to_group_create(pkg_t* retPkg, char* errmsg){
	printf("[请输入群名]");
	char grp[1024]={0};
	fgets(grp,sizeof(grp),stdin);
	grp[strlen(grp)-1]=0;
	//打包                                                                                            
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_GRP_ADD,2, grp,(char*)client_loginname());
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);
	if(ret != 0){
		print_red2("通信异常", errmsg);
		return page_to_group_index;
	}
	if(strcmp(pkg_get(retPkg, 0), "OK") != 0){
		print_red2("群创建失败", pkg_get(retPkg, 1));
	}
	print_green("聊天群创建成功");
	printf("%s\n", pkg_get(retPkg, 1));
	return page_to_group_index; 
}

void* page_to_group_add(pkg_t* retPkg, char* errmsg){
	printf("[请输入群名]");
	char grp[1024]={0};
	fgets(grp,sizeof(grp),stdin);
	grp[strlen(grp)-1]=0;
	printf("[请输入要添加的成员]");
	char mem[1024]={0};
	fgets(mem,sizeof(mem),stdin);
	mem[strlen(mem)-1]=0;
	//打包                                                                                            
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_GRP_ADD,2, grp, mem);
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);
	if(ret != 0){
		print_red2("通信异常", errmsg);
		return page_to_group_index;
	}
	if(strcmp(pkg_get(retPkg, 0), "OK") != 0){
		print_red2("添加群成员失败", pkg_get(retPkg, 1));
	}
	print_green("群成员添加成功");
	printf("%s\n", pkg_get(retPkg, 1));
	return page_to_group_index; 
}
